<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Homework;
use think\Db;

/**
 * 教材接口
 */
class Textbook extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $tb = 'text_books';//教材表
    protected $tbsp = 'textbook_synchronization_products';//教材同步表
    protected $tsc = 'textbook_synchronization_catalogues';//教材单元表
    protected $tsr = 'textbook_synchronization_resources';//教材单元单词表
    protected $tbsplog = 'textbook_synchronization_products_log';//教材切换记录表
    protected $tsclog = 'textbook_synchronization_catalogues_log';//教材切换单元表

    /**
     * 教材
     *
     */
    public function index()
    {
        $textbook = Db::name($this->tb)->group('gradeName')->order('orderNum desc')->select();
        foreach ($textbook as &$vs) {
            $vs['children'] = Db::name($this->tb)->where('gradeName',$vs['gradeName'])->order('orderNum desc')->select();
        }
        unset($vs);
        $this->success('请求成功',$textbook);
    }

    /**
     * 教材同步记录
     *
     */
    public function textbooksp(){
        $textbook = Db::name($this->tbsp)->group('gradeName')->order('gradeId asc')->select();
        foreach ($textbook as &$vs) {
            $vs['children'] = Db::name($this->tbsp)->where('gradeName',$vs['gradeName'])->order('book_id asc')->select();
        }
        unset($vs);
        $this->success('请求成功',$textbook);
    }

    /**
     * 教材同步学生端
     *
     */
    public function textbookstudent(){
        $textbook = Db::name($this->tbsp)->group('gradeName2')->order('gradeId asc')->select();
        foreach ($textbook as &$vs) {
            $vs['children'] = Db::name($this->tbsp)->where('gradeName2',$vs['gradeName2'])->order('book_id asc')->select();
        }
        unset($vs);
        $this->success('请求成功',$textbook);
    }


    /**
     * 切换教材同步
     *
     */
    /*public function  switchtextbooksp(){


        $where = [
            'user_id' => $this->auth->id,
        ];
        $item = Db::name($this->tbsplog)->where($where)->find();
        if(!empty($item)){
            $data = [
                'versionId'=>input('versionId'),
            ];
            Db::name($this->tbsplog)->where($where)->update($data);
        }else{
            $data = [
                'versionId'=>$source_id = input('versionId'),
                'user_id' =>$this->auth->id
            ];
            Db::name($this->tbsplog)->insert($data);
        }
        $this->success('成功');

    }*/


    /**
     * 教材单元列表
     *
     */
    public function unit(){
        $id = input('id/d');
        $unitlist = Db::name($this->tsc)->where('versionId',$id)->order('display_order desc')->select();
        $this->success('请求成功',$unitlist);
    }

    /**
     * 切换教材同步
     *
     */
    public function  switchunit(){
        $where = [
            'user_id' => $this->auth->id,
        ];
        $item = Db::name($this->tsclog)->where($where)->find();
        if(!empty($item)){
            $data = [
                'versionId'=>input('versionId'),
                'unitid'=>input('unitid'),
            ];
            Db::name($this->tsclog)->where($where)->update($data);
        }else{
            $data = [
                'versionId'=>input('versionId'),
                'unitid'=>$source_id = input('unitid'),
                'user_id' =>$this->auth->id
            ];
            Db::name($this->tsclog)->insert($data);
        }
        $this->success('成功');

    }


    /**
     * 教材单元单词列表
     *
     */
    public function unitword(){
        $id = input('id/d');
        $wordlist = Db::name($this->tsr)->where('unitid',$id)->select();
        $homework = new Homework();
        foreach ($wordlist as &$vs) {
        	$vs['resourceUrl_full'] = cdnurl($vs['resourceUrl'],true);
            $resource = json_decode(file_get_contents($vs['resourceUrl_full'].'/resource/resource.json'),true);
            $vs['tnum'] = count($resource);
            // $vs['tnum'] = 0;
            $vs['totalScore'] = 0;
            $vs['minute'] = $vs['tnum']*3;
            $vs['scoringrate'] = '0%';
            $vs['usenum'] = $homework->where(['uid'=>$this->auth->id,'ridjson'=>['like','%'.$vs['versionId'].'%'],'status'=>1])->count();
        }
        unset($vs);
        $this->success('请求成功',$wordlist);
    }

    /**
     * 教材单元单词详情
     *
     */
    public function wordinfo(){
        $id = input('id/d');
        $wordinfo = Db::name($this->tsr)->where('versionId',$id)->find();
        $wordinfo['resourceUrl'] = cdnurl($wordinfo['resourceUrl'],true);
        $resource = json_decode(file_get_contents($wordinfo['resourceUrl'].'/resource/resource.json'),true);
        $data = [
            'resmain' => cdnurl('/file/textbook_synchronization/res/',true),
            'resource'=>$resource,
            // 'resource_h5'=>$resource_h5,
        ];
        $this->success('请求成功',$data);
    }

    /**
     * 教材搜索
     *
     */
    public function search(){
        $key = input('key/s');
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        // if (!$key) {
        //      $this->error('未输入关键词');
        // }
        $where = [];
        if ($key) {
            $where['productName'] = ['like','%'.$key.'%']; 
        }
        
        $query1 = Db::name('hear_say_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query1 as &$vv) {
            $vv['module'] = 'hearsay';
        }
        unset($vv);
        $query2 = Db::name('listening_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query2 as &$vv) {
            $vv['module'] = 'listening';
        }
        unset($vv);
        $query3 = Db::name('repeat_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query3 as &$vv) {
            $vv['module'] = 'repeat';
        }
        unset($vv);
        $query4 = Db::name('writing_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query4 as &$vv) {
            $vv['module'] = 'writing';
        }
        unset($vv);
        $query5 = Db::name('soundmark_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query5 as &$vv) {
            $vv['module'] = 'soundmark';
        }
        unset($vv);
        $query6 = Db::name('model_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query6 as &$vv) {
            $vv['module'] = 'modelsp';
        }
        unset($vv);
        $query7 = Db::name('new_concept_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query7 as &$vv) {
            $vv['module'] = 'newconcept';
        }
        unset($vv);
        $query8 = Db::name('black_plum_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query8 as &$vv) {
            $vv['module'] = 'blackplum';
        }
        unset($vv);
        $start = ($page-1)*$pageSize; // 当前页的起始索引
        $end = $pageSize; // 当前页的结束索引
        $result = array_slice(array_merge($query1,$query2,$query3,$query4,$query5,$query6,$query7,$query8),$start,$end);
        foreach ($result as &$vs) {
            $vs['cover_full'] = cdnurl($vs['cover'],true);
        }
        unset($vs);
        $this->success('请求成功',$result);
    }

    /**
     * 教材内容详情展示
     * wei
     * 202406091511
     */
    public function contentinfo(){
        $id = input('id/d');
        $contentinfo = Db::name($this->tsr)->where('versionId',$id)->find();
        $contentinfo['resourceUrl'] = cdnurl($contentinfo['resourceUrl'],true);
        $resource = json_decode(file_get_contents($contentinfo['resourceUrl'].'/resource/resource.json'),true);
        $data = [
            'resmain' => cdnurl('/file/textbook_synchronization/res/',true),
            'resource'=>$resource,
        ];
        $this->success('请求成功',$data);
    }
}
